package sctu.edu.java.year2016.team02.project.pc.UI;

import sctu.edu.java.year2016.team02.project.pc.loginMan.PcOperator;
import sctu.edu.java.year2016.team02.project.pc.problem.PcProblem;
import sctu.edu.java.year2016.team02.project.pc.problem.SocketListener;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Eoly on 2016/6/11.
 */
//对问题进行搜索的搜索界面
public class SearchUI extends JDialog implements TextListener,ListSelectionListener{
    private TextField MainMesField;
    private JLabel MaMeMessage;
    private JList list;
    private List<Object[]> lt;
    private PcOperator pc;
    private PcProblem pb;
    private DefaultListModel model;
    private JScrollPane scroForList;

    public SearchUI(JFrame praent, String title, PcOperator pc,boolean modal) throws SQLException {
        super(praent,title,modal);
        this.pc=pc;
        Init();
        add(MainMesField);
        add(MaMeMessage);
        add(scroForList);
        setLayout(null);
        setBounds(SocketListener.screenWidth / 3, SocketListener.ScreenHeight / 4, 600, 400);
        setResizable(false);
        setVisible(true);
    }

    private void Init() throws SQLException {
        MainMesField = new TextField("");
        MaMeMessage = new JLabel("输入关键字：");
        list = new JList();
        model = new DefaultListModel();
        list = new JList(model);
        scroForList =new JScrollPane(list);
        pb = new PcProblem();
        MaMeMessage.setBounds(75, 45, 100, 25);
        MainMesField.setBounds(75, 75, 450, 25);
        scroForList.setBounds(75, 105, 450, 200);
        MainMesField.addTextListener(this);
        list.addListSelectionListener(this);
        creatList();
    }

    private void creatList() throws SQLException {
        int count=0;
        pb.setMainMessage(MainMesField.getText());
        lt = pb.seeking();
        Iterator<Object[]> it = lt.iterator();
        if(!model.isEmpty()){
            count=0;
            model.removeAllElements();
        }
        while (it.hasNext()&&count<6) {
            Object[] k=it.next();
            //k[2] 问题描述 k[5] 是否解决
            model.addElement(FormalOf("问题描述："+k[2],18)+FormalOf("是否解决："+PcProblem.isSolved(k[5]),12));
            count++;
        }
    }

    private String FormalOf(String str,int forNum){
        str = "　"+str;
        if(str.length()<forNum+1) {
            while (str.length() < forNum+1) {
                str = str + "　";
            }
        }else{
            char k[]=str.toCharArray();
            str = new String(k,0,forNum)+"…";
        }
        return str;
    }

    @Override
    public void textValueChanged(TextEvent e) {
        if(e.getSource()==MainMesField){
            try {
                creatList();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }

        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        if(!list.isSelectionEmpty()) {
            JFrame newJF = new JFrame();
            new WatchUI(newJF, "Watching", lt.get(list.getSelectedIndex()), true, pc);
            newJF.dispose();
        }
    }
}
